package cn.laixueit.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.laixueit.pojo.Category;
import cn.laixueit.service.CategoryService;
import cn.laixueit.mapper.CategoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Override
    public List<Category> listCategory(String belongType,String pid,String name) {
        List<Category> categoryList = list(lambdaQuery()
                .eq(StringUtils.isNotBlank(belongType),Category::getBelongType,belongType)
                .like(StringUtils.isNotBlank(name),Category::getName,name)
                .eq(StringUtils.isNotBlank(pid),Category::getPid,pid)
                .getWrapper());
        return categoryList;
    }

    @Override
    public List<Map<String,Object>> listTree(String belongType) {
        List<Category> categoryList = listCategory(belongType,null,null);

        if(CollectionUtil.isNotEmpty(categoryList)){
            LinkedList<Map<String, Object>> tree = buildTree(categoryList,0);
            return tree;
        }
        return new ArrayList<>();
    }

    @Override
    public void saveCategory(Category category) {
        saveOrUpdate(category);
    }

    @Override
    public void removeCategory(Integer id) {
        int count = count(lambdaQuery()
                .apply(String.format("find_in_set(%d,pids)",id))
                .getWrapper());
        if(count>0){
            throw new RuntimeException("存在子级分类");
        }
        removeById(id);
    }

    /**
     * 递归查询分类
     * @param categoryList 数据集合
     * @param pid 父类id
     * @return
     */
    public LinkedList<Map<String, Object>> buildTree(List<Category> categoryList,Integer pid){
        LinkedList<Map<String, Object>> linkedList = new LinkedList<>();

        List<HashMap<String, Object>> list = categoryList.stream().map(x -> {
            HashMap<String, Object> map = new HashMap<>();
            Integer pid1 = x.getPid();
            if(pid1 == pid){
                Integer id = x.getId();
                map.put("id",x.getId());
                map.put("label",x.getName());
                map.put("value", id);
                map.put("pid", pid1);
                map.put("pids",x.getPids());
                map.put("level",x.getLevel());
                LinkedList<Map<String, Object>> buildTree = buildTree(categoryList, id);
                if(CollectionUtil.isNotEmpty(buildTree)){
                    map.put("children",buildTree);
                }
                return map;
            }
            return null;
        }).filter(x-> ObjectUtil.isNotEmpty(x)).collect(Collectors.toList());

        linkedList = new LinkedList<>(list);

        return linkedList;
    }
}




